using System;
using System.Collections.Generic;
using System.Text;
using Dominio;
using NHibernate.Cfg;
using NHibernate;

namespace DAP
{
    public class BugRepository : AbstractRepository
    {
        public BugRepository(): base()
        {
        }

        public IList<Bug> getBugList()
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
            IQuery query = sesion.CreateQuery("from Bug");
            transaction.Commit();
            return query.List<Bug>();
            }
            catch(Exception){
                transaction.Rollback();
                throw new HibernateException();
            }
        }

        public IList<Bug> getFilteredBugList(String tipo, String status, String key)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
                String queryString = "from Bug Bug where Bug.Description like '%" + key + "%'";
                if (!tipo.Equals("All"))
                {
                    queryString = queryString + " and Bug.Tipo = '" + tipo + "'";
                }
                if (!status.Equals("All"))
                {
                    queryString = queryString + " and Bug.Status ='" + status + "'";
                }
                IQuery query = sesion.CreateQuery(queryString);
                transaction.Commit();
                return query.List<Bug>();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw new HibernateException();
            }
        }
        public void SaveBug(Bug bug) 
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
                sesion.Save(bug);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw new HibernateException();
            }
        }
        public void UpdateBug(Bug bug)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
                sesion.Update(bug);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw new HibernateException();
            }
        }



        public Bug getBugById(int Id)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try
            {
                IQuery query = sesion.CreateQuery("from Bug where Id = :x");
                query.SetString("x", Id.ToString());
                transaction.Commit();
                return (Bug)query.UniqueResult();
            }
            catch
            {
                transaction.Rollback();
                throw new HibernateException();
            }
        }

    }
}
